﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.OleDb;
using System.Data.SqlClient;


namespace WebApplication1
{
    public partial class LoguerMenage : System.Web.UI.Page
    {
        protected const string connectionString = @"Provider=SQLOLEDB;server=FORMA1614;Database=magicrecettes;User ID=magicrecettes;Password=cook;Trusted_Connection=False";
        protected const string query = "SELECT * FROM Menage";


        protected void Page_Load(object sender, EventArgs e)
        {
            LblUserNonExist.Text = "";
            LblPasswordWrong.Text = "";
        }

        protected void btnValider_Click(object sender, EventArgs e)
        {
            OleDbConnection connection = new OleDbConnection(connectionString);
            connection.Open();
            OleDbCommand command = new OleDbCommand(query, connection);
            OleDbDataReader dr = command.ExecuteReader();
            bool trouve = false;

            if (dr.HasRows)
                while (dr.Read() && trouve==false)
                    if (txtLogin.Text == dr.GetString(1))
                        if (txtPassword.Text == dr.GetString(2))
                        {
                            //Au cas où l'utilisateur,deja logué,se relogue avec un autre compte ou le sien. 
                            Session["login"] = null;
                            Session["nom"] = null;
                            Session["prenom"] = null;

                            //Session Start 
                            Session["login"] = dr.GetString(1);
                            Session["nom"] = dr.GetString(4);
                            Session["prenom"] = dr.GetString(5);
                            

                            Response.Redirect("~/Bienvenue.aspx");
                            trouve = true;
                        }
                        else
                        {
                            LblPasswordWrong.Text="mauvais mot de passe*";
                            trouve = true;
                        }

            if (trouve == false)
                LblUserNonExist.Text="Utilisateur inexistant*";
            dr.Close();
            connection.Close();
        }
    }
}